<template>
  <div class="box order-status">
    <!-- 订单待付款 -->
    <div class="status" v-if="orderDetail.status === 1">
      <h1 v-if="countdown">请在{{ countdown }}内支付</h1>
      <h1 v-else>订单已取消</h1>
      <div class="deliveryTime">
        预计送达时间<span style="color: #ff0000; margin-left: 5px">{{ deliveryTime }}</span>
      </div>
    </div>
    <div class="status" v-else>
      <h1>{{ '订单' + getOrderStatus(orderDetail.status) }}</h1>
      <!-- 订单已完成 -->
      <span v-if="orderDetail.status === 5"> 感谢您对本平台的信任，期待再次光临。请厉行节约，拒绝浪费 </span>
      <!-- 订单已取消 -->
      <span v-if="orderDetail.status === 6">{{ orderDetail.cancelReason }}</span>
    </div>
    <div class="more">
      <div class="left"></div>
      <div class="right">
        <!-- 订单待付款 -->
        <div class="btn" v-if="orderDetail.status === 1">
          <button class="evaluate" @click="handleCancel">取消订单</button>
          <button class="payment" @click="handlePayment">立即支付￥{{ orderDetail.amount }}</button>
        </div>
        <!-- 订单已完成 -->
        <div class="btn" v-if="orderDetail.status === 5">
          <button class="evaluate" @click="openEvaluatePopup">去评价</button>
          <button class="again" @click="handleAgain">再来一单</button>
        </div>
        <!-- 订单已取消 -->
        <div class="btn" v-if="orderDetail.status === 6">
          <button class="again" @click="handleAgain">再来一单</button>
        </div>
      </div>
    </div>
  </div>
  <!-- 评价底部弹窗 -->
  <van-popup v-model:show="showEvaluatePopup" position="bottom">
    <div class="evaluate-popup">
      <h2 class="popup-title">评价订单</h2>
      <textarea v-model="evaluationContent" class="evaluation-textarea" placeholder="请输入您的评价"></textarea>
      <div class="button-group">
        <van-button type="primary" @click="submitEvaluation">提交评价</van-button>
      </div>
    </div>
  </van-popup>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { OrderDetailResult } from '@/types/order'
import { getOrderStatus } from '../constant'
import dayjs from 'dayjs'
import { computed } from 'vue'
import * as commentApi from '@/api/comment'

const props = defineProps<{
  orderDetail: OrderDetailResult
  countdown: string
}>()
const emits = defineEmits<{
  (e: 'openPopup'): void
  (e: 'cancelClick'): void
  (e: 'againClick'): void
  (e: 'deleteClick'): void
}>()
// 预计到达时间
const deliveryTime = computed(() => {
  return dayjs(props.orderDetail.estimatedDeliveryTime).format('HH:mm')
})
const handleCancel = () => {
  emits('cancelClick')
}
const handlePayment = () => {
  emits('openPopup')
}
const handleAgain = () => {
  emits('againClick')
}
// 控制评价弹窗的显示状态
const showEvaluatePopup = ref(false)
// 评价内容
const evaluationContent = ref('')
const openEvaluatePopup = () => {
  console.log(123)

  showEvaluatePopup.value = true
}
const submitEvaluation = async () => {
  try {
    const res = await commentApi.addCommentAPI({
      orderId: props.orderDetail.id,
      comment: evaluationContent.value,
      dishId: props.orderDetail.orderDetailList[0].dishId,
    })
  } catch (error) {
    console.log(error)
  }
  showEvaluatePopup.value = false
  evaluationContent.value = ''
}
</script>
<style scoped lang="scss">
.order-status {
  padding: 10px;
}
.status {
  font-size: var(--font-size-big);
  .deliveryTime,
  span {
    margin-top: 5px;
    margin-bottom: 10px;
    display: inline-block;
    color: #999;
    font-size: var(--font-size-small);
  }
}
.more {
  margin-top: 5px;
  font-size: var(--font-size-small);
  display: flex;
  justify-content: space-between;
  .right {
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
  }
  button {
    background: var(--box-background);
    padding: 6px;
    border-radius: 20px;
  }
  .evaluate {
    border: 1px solid #ccc;
    margin-right: 10px;
  }
  .again,
  .payment {
    color: var(--red-text);
    border: 1px solid var(--red-text);
  }
  .payment {
    padding: 6px 20px;
  }
}
.evaluate-popup {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.popup-title {
  font-size: 20px;
  font-weight: bold;
  text-align: center;
  color: #333;
}

.evaluation-textarea {
  width: 100%;
  height: 150px;
  padding: 10px;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  resize: none;
  font-size: 14px;
}
.button-group {
  display: flex;
  justify-content: flex-end;
}
</style>
